/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.stock;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.library.internal.base.ArrayAdapter;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class ListStockAdapter extends ArrayAdapter<Stock> {


    public ListStockAdapter(Context context, List<Stock> objects) {
        super(context, 0, objects);
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {

        Stock item = getItem(position);
        //Without ViewHolder for demo purpose
        Component view = convertView;
        if (view == null) {
            LayoutScatter li = LayoutScatter.getInstance(getContext());
            view = li.parse(ResourceTable.Layout_carddemo_googlenowstock_list_item, parent, false);
        }
        Text Text1 = (Text) view.findComponentById(ResourceTable.Id_Text1);
        Text Text2 = (Text) view.findComponentById(ResourceTable.Id_Text2);
        Text Text3 = (Text) view.findComponentById(ResourceTable.Id_Text3);
        Text Text4 = (Text) view.findComponentById(ResourceTable.Id_Text4);

        Text1.setText(item.code);
        Text2.setText("" + item.value);
        Text3.setText("" + item.delta);
        Text4.setText("" + item.deltaPerc + "%");


        return view;
    }

    @Override
    public int getComponentTypeCount() {
        return 1;
    }

}